# include <iostream>
using namespace std;
# include <vector>

void Hannt(vector<int>& begin, vector<int>& mid, vector<int>& fin, int n)
{
    if (n == 1)
    {
        fin.push_back(begin.back());
        begin.pop_back();
        return;
    }

    Hannt(begin, fin, mid, n - 1);
    Hannt(begin, mid, fin, 1);
    Hannt(mid, begin, fin, n - 1);
}
void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
    Hannt(A, B, C, A.size());
}

int main()
{
    vector<int>a({6,5,4,3,2,1}), b, c;
    hanota(a, b, c);
    for (int x : c)
    {
        cout << x << " ";
    }
}

ListNode* Getsorthead(ListNode* list1, ListNode* list2)
{
    if (list1 == nullptr)
    {
        return list2;
    }
    if (list2 == nullptr)
    {
        return list1;
    }
    if (list1->val <= list2->val)
        list1->next = Getsorthead(list1->next, list2);
    else
        list2->next = Getsorthead(list1, list2->next);

}
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
    return  Getsorthead(list1, list2);
}

typedef struct ListNode List;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    if (list1 == NULL)
        return list2;
    if (list2 == NULL)
        return list1;
    List* newlist, * tail;
    newlist = tail = (List*)malloc(sizeof(List));
    while (list1 && list2)
    {
        if (list1->val < list2->val)
        {
            tail->next = list1;
            tail = tail->next;
            list1 = list1->next;
        }
        else
        {
            tail->next = list2;
            tail = tail->next;
            list2 = list2->next;
        }
    }

    while (list1)
    {
        tail->next = list1;
        tail = tail->next;
        list1 = list1->next;
    }

    while (list2)
    {

        tail->next = list2;
        tail = tail->next;
        list2 = list2->next;
    }
    List* ZZ = newlist->next;
    free(newlist);
    newlist = NULL;
    return ZZ;
}